﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Bank_database_system
{
    public partial class 支行信息a : UserControl
    {
        支行信息b branch_inf_b = new 支行信息b();
        支行信息c branch_inf_c = new 支行信息c();
        public 支行信息a()
        {
            InitializeComponent();
            // 订阅支行信息b的返回事件
            branch_inf_b.ButtonBack += BackToA;
            branch_inf_c.ButtonBack += BackToA;
        }
        private void BackToA(object sender, EventArgs e)
        {
            // 修改支行信息a中的控件状态
            panel1.Visible = false;
            panel1.Controls.Clear();
            branch_inf_b = new();
            branch_inf_c = new();
            branch_inf_b.ButtonBack += BackToA;
            branch_inf_c.ButtonBack += BackToA;
            button1_Click(this, e);
        }
        private void button9_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
            panel1.Controls.Clear();
            panel1.Controls.Add(branch_inf_b);
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void 支行信息a_Load(object sender, EventArgs e)
        {
            button1_Click(this, EventArgs.Empty);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
            panel1.Controls.Clear();
            panel1.Controls.Add(branch_inf_c);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Clear();
            textBox9.Clear();
            textBox10.Clear();
            textBox1.Clear();
            dataGridView1.DataSource = null;
            button1_Click(sender, e);
        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox9_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // textBox9 银行id
            // textBox2 银行名称
            // textBox10 地址
            // textBox1 电话号码

            string bankId = string.IsNullOrEmpty(textBox9.Text) ? null : textBox9.Text;
            string bankName = string.IsNullOrEmpty(textBox2.Text) ? null : textBox2.Text;
            string address = string.IsNullOrEmpty(textBox10.Text) ? null : textBox10.Text;
            string phoneNumber = string.IsNullOrEmpty(textBox1.Text) ? null : textBox1.Text;
            int page = Convert.ToInt32(label6.Text);


            try
            {
                int maxPage = FunctionsforITDepartment.GetBranchTotalPageCount(bankId, bankName, address, phoneNumber, 20);
                label5.Text = Convert.ToString(maxPage > 1 ? maxPage : 1);

                DataTable staffData = FunctionsforITDepartment.GetBranch(bankId, bankName, address, phoneNumber, page, 20);

                var filteredRows = staffData.AsEnumerable()
                                            .Where(row => !row.Field<string>("Branch_Name").Equals("系统", StringComparison.OrdinalIgnoreCase))
                                            .ToList();

                if (filteredRows.Any())
                {
                    staffData = filteredRows.CopyToDataTable();
                }
                else
                {
                    staffData = staffData.Clone(); // 或者创建一个空的 DataTable
                }

                dataGridView1.RowHeadersVisible = false;
                dataGridView1.AutoGenerateColumns = false; // 禁用自动生成列
                dataGridView1.AllowUserToAddRows = false; // 禁用添加空白行
                dataGridView1.Columns.Clear(); // 清除已有列
                dataGridView1.DoubleBuffered(true); //双缓冲

                // 添加 BRANCH_ID 列
                DataGridViewTextBoxColumn branchIdColumn = new DataGridViewTextBoxColumn
                {
                    Name = "BRANCH_ID",
                    HeaderText = "支行ID",
                    DataPropertyName = "BRANCH_ID",
                    DefaultCellStyle = new DataGridViewCellStyle
                    {
                        Alignment = DataGridViewContentAlignment.MiddleCenter
                    },
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(branchIdColumn);

                // 添加 BRANCH_NAME 列
                DataGridViewTextBoxColumn branchNameColumn = new DataGridViewTextBoxColumn
                {
                    Name = "BRANCH_NAME",
                    HeaderText = "支行名称",
                    DataPropertyName = "BRANCH_NAME",
                    DefaultCellStyle = new DataGridViewCellStyle
                    {
                        Alignment = DataGridViewContentAlignment.MiddleCenter
                    },
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(branchNameColumn);

                // 添加 BRANCH_ADDRESS 列
                DataGridViewTextBoxColumn branchAddressColumn = new DataGridViewTextBoxColumn
                {
                    Name = "BRANCH_ADDRESS",
                    HeaderText = "地址",
                    DataPropertyName = "BRANCH_ADDRESS",
                    DefaultCellStyle = new DataGridViewCellStyle
                    {
                        Alignment = DataGridViewContentAlignment.MiddleCenter
                    },
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(branchAddressColumn);

                // 添加 BRANCH_PHONENUMBER 列
                DataGridViewTextBoxColumn branchPhoneNumberColumn = new DataGridViewTextBoxColumn
                {
                    Name = "BRANCH_PHONENUMBER",
                    HeaderText = "电话号码",
                    DataPropertyName = "BRANCH_PHONENUMBER",
                    DefaultCellStyle = new DataGridViewCellStyle
                    {
                        Alignment = DataGridViewContentAlignment.MiddleCenter
                    },
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(branchPhoneNumberColumn);

                DataGridViewButtonColumn modifyButtonColumn = new DataGridViewButtonColumn
                {
                    Name = "Modify",
                    HeaderText = "修改信息",
                    Text = "修改",
                    UseColumnTextForButtonValue = true,
                    Width = 100,
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(modifyButtonColumn);


                dataGridView1.CellClick += DataGridView1_CellClick;
                dataGridView1.DataSource = staffData; // 绑定数据

                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // 列宽自动调整模式
            }
            catch (Exception ex)
            {
                // 错误处理

            }
        }
        private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // 确保点击的是数据行和按钮列
            if (e.RowIndex >= 0 && e.ColumnIndex == dataGridView1.Columns["Modify"].Index)
            {
                // 获取当前行的数据
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                string branchId = row.Cells["BRANCH_ID"].Value?.ToString();
                string name = row.Cells["BRANCH_NAME"].Value?.ToString();
                string address = row.Cells["BRANCH_ADDRESS"].Value?.ToString();
                string phoneNumber = row.Cells["BRANCH_PHONENUMBER"].Value?.ToString();

                // 打开修改页面并传递数据
                OpenModifyPage(branchId, name, address, phoneNumber);
            }
        }

        private void OpenModifyPage(string branchId, string name, string address, string phoneNumber)
        {
            panel1.Visible = true;
            panel1.Controls.Clear();
            panel1.Controls.Add(branch_inf_b);
            branch_inf_b.SetBranchInfo(branchId, name, address, phoneNumber);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            int page = Convert.ToInt32(label6.Text);
            int maxPage = Convert.ToInt32(label5.Text);
            if (page > 1)
            {
                page--;
                label6.Text = Convert.ToString(page);
                button1_Click(this, EventArgs.Empty);
                if (page < maxPage)
                {
                    label6.Text = Convert.ToString(1);
                    button1_Click(this, EventArgs.Empty);
                }
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            int page = Convert.ToInt32(label6.Text);
            int maxPage = Convert.ToInt32(label5.Text);
            if (page < maxPage)
            {
                page++;
                label6.Text = Convert.ToString(page);
                button1_Click(this, EventArgs.Empty);
            }
        }
    }
}
